package com.lepu.app.fun.mother_circle.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.utils.LoginUtil;
import com.app.utils.Setting;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.main.DateUtilBase;
import com.core.lib.utils.main.UIHelper;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.core.lib.widget.CircularImageView;
import com.eyzhs.app.R;
import com.lepu.app.fun.mother_circle.MotherTopicActivity;
import com.lepu.app.fun.mother_circle.bean.BeanMotherTopic;
import com.lepu.app.fun.my.PubContentDetailActivity;

public class AdapterMotherTopic extends BaseAdapter {

	private LayoutInflater mLayoutInflater = null;
	public Context mContext = null;
	public ArrayList<BeanMotherTopic> mArrayList = null;
	public int mTopicType = 1; //1 代表全部   2代表最新  3代表精品

	public AdapterMotherTopic(Context context,ArrayList<BeanMotherTopic> arrayList, int topicType) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
		mTopicType = topicType;
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.size() > 0) {
			size = mArrayList.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;

		if (convertView == null) {
			convertView = mLayoutInflater.inflate(R.layout.mother_topic_listitem, null);

			holder = new ViewHolder();

			LinearLayout mainItemLayout = (LinearLayout) convertView.findViewById(R.id.mainItemLayout);
			LinearLayout mainItemHeadLayout = (LinearLayout) convertView.findViewById(R.id.mainItemHeadLayout);
			LinearLayout mainItemMarginLayout = (LinearLayout) convertView.findViewById(R.id.mainItemMarginLayout);
			TextView headTextView = (TextView) convertView.findViewById(R.id.topic_head_item_text);
			
			CircularImageView iconImageView = (CircularImageView) convertView.findViewById(R.id.iconImageView);
			
			TextView userNameTextView = (TextView) convertView.findViewById(R.id.topic_item_name);
			TextView timeTextView = (TextView) convertView.findViewById(R.id.topic_item_time);
			TextView commentCountTextView = (TextView) convertView.findViewById(R.id.topic_item_comment);
			TextView lookCountTextView = (TextView) convertView.findViewById(R.id.topic_item_look);
			
			TextView titleTextView = (TextView) convertView.findViewById(R.id.topic_item_title);
			TextView contentTextView = (TextView) convertView.findViewById(R.id.topic_item_content);
			ImageView goodImageView = (ImageView) convertView.findViewById(R.id.topic_item_good_icon);
			ImageView imgImageView = (ImageView) convertView.findViewById(R.id.topic_item_img_icon);
			
			ImageView headGoodImageView = (ImageView) convertView.findViewById(R.id.headGoodIcon);
			ImageView headHotImageView = (ImageView) convertView.findViewById(R.id.headHotIcon);

			holder.mainItemLayout = mainItemLayout;
			holder.mainItemHeadLayout = mainItemHeadLayout;
			holder.mainItemMarginLayout = mainItemMarginLayout;
			holder.headTextView = headTextView;
			holder.iconImageView = iconImageView;
			
			holder.userNameTextView = userNameTextView;
			holder.timeTextView = timeTextView;
			holder.commentCountTextView = commentCountTextView;
			holder.lookCountTextView = lookCountTextView;
			
			holder.titleTextView = titleTextView;
			holder.contentTextView = contentTextView;
			holder.goodImageView = goodImageView;
			holder.imgImageView = imgImageView;
			
			holder.headGoodImageView = headGoodImageView;
			holder.headHotImageView = headHotImageView;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		holder.mainItemLayout.setTag(position);

		final BeanMotherTopic item = mArrayList.get(position);
		
		holder.mainItemMarginLayout.setVisibility(View.GONE);
		
        if(item.topicIsTop == 1 && mTopicType == 1){  //是否为置顶
        	holder.mainItemLayout.setVisibility(View.GONE);
        	holder.mainItemHeadLayout.setVisibility(View.VISIBLE);

    		if(item.topicIsGood == 1){
    			holder.headGoodImageView.setVisibility(View.VISIBLE);
    			if(item.topicIsHot == 1){
    				holder.headHotImageView.setVisibility(View.VISIBLE);
    				holder.headTextView.setText("                  "+item.topicTitle);
    			}else{
    				holder.headHotImageView.setVisibility(View.GONE);
    				holder.headTextView.setText("            "+item.topicTitle);
    			}
    		    
    			
    		}else{
    			holder.headGoodImageView.setVisibility(View.GONE);
    			if(item.topicIsHot == 1){
    				holder.headHotImageView.setVisibility(View.VISIBLE);
    				holder.headTextView.setText("            "+item.topicTitle);
    			}else{
    				holder.headHotImageView.setVisibility(View.GONE);
    				holder.headTextView.setText("      "+item.topicTitle);
    			}
    			
    		}
        	
        	holder.mainItemHeadLayout.setOnClickListener(new View.OnClickListener() {

    			@Override
    			public void onClick(View v) {
    				Intent intent = new Intent(mContext,PubContentDetailActivity.class);
    				intent.putExtra("tag", 1);
    				intent.putExtra("TopicId", item.topicID);
    				intent.putExtra("nameType", 1); //置顶
    				
    				((MotherTopicActivity)mContext).startActivity(intent, true);
    			}
    		});
        }else{
        	holder.mainItemLayout.setVisibility(View.VISIBLE);
        	holder.mainItemHeadLayout.setVisibility(View.GONE);
        	
        	if(position > 0 && mArrayList.get(position-1).topicIsTop == 1 && mTopicType == 1){
        		holder.mainItemMarginLayout.setVisibility(View.VISIBLE);
        	}else{
        		holder.mainItemMarginLayout.setVisibility(View.GONE);
        	}
        	
    		String imageUrl = Setting.getImageUrl(item.topicAvatar);
    		//UniversalImageLoadTool.disPlay(imageUrl, holder.iconImageView, R.drawable.app_ic_default_avatar_64);
			LoginUtil.loadSelfUserImage(mContext, holder.iconImageView, imageUrl);

    		holder.userNameTextView.setText(item.topicUserName);
    		holder.commentCountTextView.setText(" "+item.topicCommentCount);
    		holder.lookCountTextView.setText(" "+item.topicLookCount);
    		
            holder.contentTextView.setText(item.topicContent);
            UIHelper.setTextViewLines(holder.contentTextView, 2);

//            //内容 隐藏
//            if(mTopicType !=3){
//            	holder.contentTextView.setVisibility(View.GONE);
//            }
            
    		boolean hasDate = false;
    		for (int i = 0; i < item.MediaList.size(); i++) {
    			if (!TextUtils.isEmpty(item.MediaList.get(i).MediaUri)) {
    				hasDate = true;
    			}
    		}
            
    		if(item.topicIsGood == 1){
    			holder.goodImageView.setVisibility(View.VISIBLE);
    			if(hasDate){
    				holder.imgImageView.setVisibility(View.VISIBLE);
    				holder.titleTextView.setText("            "+item.topicTitle);
    			}else{
    				holder.imgImageView.setVisibility(View.GONE);
    				holder.titleTextView.setText("      "+item.topicTitle);
    			}
    			
    		}else{
    			holder.goodImageView.setVisibility(View.GONE);
    			if(hasDate){
    				holder.imgImageView.setVisibility(View.VISIBLE);
    				holder.titleTextView.setText("      "+item.topicTitle);
    			}else{
    				holder.imgImageView.setVisibility(View.GONE);
    				holder.titleTextView.setText(""+item.topicTitle);
    			}
    			
    		}
            
            UIHelper.setTextViewLines(holder.titleTextView, 2);
            
            //time
            if(mTopicType == 1){
            	holder.timeTextView.setText(DateUtilBase.friendly_time(item.topicReplyTime));
            }else{
            	holder.timeTextView.setText(DateUtilBase.friendly_time(item.topicTime));
            }
    		holder.mainItemLayout.setOnClickListener(new View.OnClickListener() {

    			@Override
    			public void onClick(View v) {
    				Intent intent = new Intent(mContext,PubContentDetailActivity.class);
    				intent.putExtra("tag", 1);
    				intent.putExtra("TopicId", item.topicID);
    				if(item.topicIsGood == 1){
    					intent.putExtra("nameType", 2); //精华
    				}
    				
    				((MotherTopicActivity)mContext).startActivity(intent, true);
    			}
    		});
    		
    		holder.iconImageView.setOnClickListener(new View.OnClickListener() {

    			@Override
    			public void onClick(View v) {
    				LoginUtil.jumpUserInfo((BaseFragmentActivity)mContext, item.topicUserId);
    			}
    		});
        }

		return convertView;
	}

	private static class ViewHolder {

		LinearLayout mainItemLayout;
		LinearLayout mainItemHeadLayout;
		LinearLayout mainItemMarginLayout;
		TextView headTextView;
		
		CircularImageView iconImageView;
		
		TextView userNameTextView;
		TextView timeTextView;
		TextView commentCountTextView;
		TextView lookCountTextView;
		
		TextView titleTextView;
		TextView contentTextView;
		
		ImageView goodImageView;
		ImageView imgImageView;
		
		ImageView headGoodImageView;
		ImageView headHotImageView;

	}
}
